﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LdCms.BLL.Community
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Community;
    using LdCms.IDAL.Community;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    public partial class ThemeReplyService:BaseService<Ld_Community_ThemeReply>,IThemeReplyService
    {
        private readonly IThemeReplyDAL ThemeReplyDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public ThemeReplyService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IThemeReplyDAL ThemeReplyDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.ThemeReplyDAL = ThemeReplyDAL;
            this.Dal = ThemeReplyDAL;
        }
        public override void SetDal()
        {
            Dal = ThemeReplyDAL;
        }

        public bool SaveThemeReply(Ld_Community_ThemeReply entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string themeId = entity.ThemeID;
                string replyId = entity.ReplyID;
                if (string.IsNullOrEmpty(replyId))
                {
                    var communityThemeReply = PrimaryKeyHelper.PrimaryKeyType.CommunityThemeReply;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V1;
                    string createThemeReplyId = PrimaryKeyHelper.MakePrimaryKey(communityThemeReply, primaryKeyLen);
                    entity.ReplyID = createThemeReplyId;
                }
                entity.UpNum = entity.UpNum.ToInt();
                entity.DownNum = entity.DownNum.ToInt();
                entity.State = entity.State.ToBool();
                entity.CreateDate = DateTime.Now;

                int intnum = 0;
                var dbContext = new DAL.BaseDAL(LdCmsDbEntitiesContext);
                var db = dbContext.DbEntities();
                var trans = db.Database.BeginTransaction();
                try
                {
                    var expressionThemeReply = ExtLinq.True<Ld_Community_ThemeReply>();
                    var expressionThemeReplyTotalNum = expressionThemeReply.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
                    var expressionThemeNotVerifyTotalNum = expressionThemeReply.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId && m.State == false);
                    int themeReplyTotalNum = dbContext.Count(expressionThemeReplyTotalNum);
                    int themeNotVerifyTotalNum = dbContext.Count(expressionThemeNotVerifyTotalNum);

                    var expressionTheme = ExtLinq.True<Ld_Community_Theme>();
                    expressionTheme = expressionTheme.And(m => m.SystemID == systemId && m.CompanyID == companyId && m.ThemeID == themeId);
                    var entityTheme = dbContext.Find(expressionTheme);

                    entityTheme.ReplyTotalNum = themeReplyTotalNum + 1;
                    entityTheme.NotVerifyTotalNum = entity.State.ToBool() ? themeNotVerifyTotalNum : themeNotVerifyTotalNum + 1;

                    dbContext.Add(entity);
                    dbContext.Update(entityTheme);
                    intnum = db.SaveChanges();
                    trans.Commit();
                }
                catch (Exception)
                {
                    trans.Rollback();
                }
                return intnum > 0;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



    }
}
